// 本文件对Block的方法进行性能测试
// @Author: zhengqi Wu
// @Description:
// @Version: 1.0.0
// @Date: 2021/4/9 13:11 下午
// @Copyright: MIN-Group；国家重大科技基础设施——未来网络北大实验室；深圳市信息论与未来网络重点实验室
//
const benchmark = require('benchmark')
const {Encoder, MaxPacketSize} = require('./Encoder')
const {Block, createBlockByTypeLengthBuffer, createBlockByBuffer} = require('./Block')


let buf = new ArrayBuffer(10000)

const suite1 = new benchmark.Suite()
suite1.add('测试Block工厂化方法性能', () => {
    createBlockByTypeLengthBuffer((241), (8000), buf.slice(0, 8000), true)
}).on('cycle', (event) => {
    console.log(String(event.target))
}).on('complete', () =>{
    console.log('Block工厂化方法1 finished')
}).run()

let view = new DataView(buf)
view.setUint8(0, 212)
view.setUint8(1, 241)
view.setUint16(2, 8000, false)
const suite2 = new benchmark.Suite()
suite2.add('测试Block工厂化方法性能', () => {
    createBlockByBuffer(buf.slice(0, 8004), true)
}).on('cycle', (event) => {
    console.log(String(event.target))
}).on('complete', () =>{
    console.log('Block工厂化方法2 finished')
}).run()

buf = new ArrayBuffer(10000)
let block1 = createBlockByTypeLengthBuffer((241), (8000), buf.slice(0, 8000), true)
let block = new Block()
const suite3 = new benchmark.Suite()
suite3.add('测试Block初始化方法性能', () => {
    block.buildByBlock(block1)
}).on('cycle', (event) => {
    console.log(String(event.target))
}).on('complete', () =>{
    console.log('Block初始化方法3 finished')
}).run()


block = new Block()
const suite4 = new benchmark.Suite()
suite4.add('Block初始化方法测试性能', () => {
    block.buildByTypeLengthBuffer((241), (8000), buf.slice(0, 8000), true)
}).on('cycle', (event) => {
    console.log(String(event.target))
}).on('complete', () =>{
    console.log('Block初始化方法4 finished')
}).run()

buf = new ArrayBuffer(10000)
view = new DataView(buf)
for(let i = 0; i < 240; i++){
    view.setUint8(i * 8, i + 1)
    view.setUint8(i*8+1, 6)
}
block = new Block()
block.buildByTypeLengthBuffer((241), (240*8), buf.slice(0, 240*8), true)
const suite5 = new benchmark.Suite()
suite5.add('Block解析测试性能', () => {
    block.parseSubElements()
}).on('cycle', (event) => {
    console.log(String(event.target))
}).on('complete', () =>{
    console.log('Block解析测试性能 finished')
}).run()